package com.xinyu.TestPackage;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import com.xinyu.entity.User;

@SuppressWarnings("deprecation")
public class Test {
	//BeanFactory当需要调用时读取配置信息，生成某个类的实例
	//ApplicationContext 在初始化时就把 xml 的配置信息读入内存 创建所有实例
	@org.junit.Test
	public void fn0() {
		//BeanFactory当需要调用时读取配置信息，生成某个类的实例
		//ApplicationContext 在初始化时就把 xml 的配置信息读入内存 创建所有实例
		//ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
		User user = (User) beanFactory.getBean("user");
		System.out.println(user);
	}
	//bean 属性 scope 默认值   singleton 
	@org.junit.Test
	public void fn1() {
		
		@SuppressWarnings("resource")
		ApplicationContext aContext =new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User) aContext.getBean("user");
		User user1 = (User) aContext.getBean("user");
		System.out.println(user);
		System.out.println(user1);
	}
	//bean 属性 scope    prototype 每次都创建新的
	@org.junit.Test
	public void fn2() {
		
		@SuppressWarnings("resource")
		ApplicationContext aContext =new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User) aContext.getBean("user");
		User user1 = (User) aContext.getBean("user");
		System.out.println(user);//com.xinyu.entity.User@12637b4
		System.out.println(user1);//com.xinyu.entity.User@13229a
	}
	
	//初始化 销毁方法 
	//ApplicationContext接口   没有销毁的方法  ClassPathXmlApplicationContext 有
	@org.junit.Test
	public void fn3() {
		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User) aContext.getBean("user");
		System.out.println(user);
		aContext.destroy();
	}
	
	
	//spring 创建对象的方式
	@org.junit.Test
	public void fn4() {
		ClassPathXmlApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User) aContext.getBean("user1");
		System.out.println(user);
	}
	
	@org.junit.Test
	public void fn5() {
		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		User user = (User) aContext.getBean("user");
		System.out.println(user);
	}
	@org.junit.Test
	public void fn6() {
		
	}
}
